// pages/comment/commentDetails.js
const app = getApp()
import Api from '../../utils/api';
import util from '../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loding: {},
    commentEntity: {}, //评价
    replyEntity: {}, //回复
    replyPage: 1,
    replySize: 10,
    replyList: [], //回复列表
    replyTotalNum: 0, //回复总数量
    replyTotalPage: 0,
    commentInputFlag: false, //评论框输入标记
    replyFlag: false, //回复框输入标记
    refreshFlag: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options.id);
    this.queryCommentDesc(options.id);
    this.queryCommentReply(options.id);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      refreshFlag: true
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    if (this.data.refreshFlag) {
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
      this.setData({
        refreshFlag: false
      });
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    app.globalData.otherPageHideFlag = true;
  },
  //请求数据更新
  onPullDownRefresh() {
    console.log('请求数据更新');
    //请求数据
    if (this.data.refreshFlag) {
      this.setData({
        refreshFlag: false
      });
      this.setData({
        replyPage: 1,
        replyList: []
      });
      wx.showNavigationBarLoading();
      this.queryCommentReply(this.data.commentEntity.id);
      this.queryCommentDesc(this.data.commentEntity.id);
    }
    setTimeout(() => {
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
      this.setData({
        refreshFlag: true
      });
    }, '1500');
  },
  //滚动触底事件
  onReachBottom: function() {
    console.log('滚动触底事件');
    let currentPage = this.data.replyPage;
    //判断当前页是否是最后一页
    if (currentPage >= this.data.replyTotalPage) {
      console.log('已到达最后一页');
      this.setData({
        replyPage: this.data.replyTotalPage
      })
      return;
    }
    this.setData({
      replyPage: ++currentPage
    }); 
    //执行查询
    this.queryCommentReply(this.data.commentEntity.id);
  },
  //查询回复列表
  queryCommentReply(commentId) {
    var _this = this;
    _this.showBottomLoding();
    Api.queryCommentReply(commentId, this.data.replyPage, this.data.replySize).then(res => {
      console.log("评价回复", res);
      let dataList = res.data.dataList;
      //格式化时间
      dataList.forEach((n,i)=>{
        n.formatDate = util.timeCalculation(new Date(n.createDate));
      })
      console.log(dataList);
      let replyList = this.data.replyList;
      replyList = replyList.concat(dataList);
      this.setData({
        replyList: replyList,
        replyTotalNum: res.data.numTotal,
        replyTotalPage: res.data.totalPages
      });
      _this.hideBottomLoding();
    }).catch(err=>{
      _this.hideBottomLoding();
    });
  },
  //查询评价详情
  queryCommentDesc(commentId) {
    var _this = this;
    Api.queryCommentDesc(commentId).then(res => {
      console.log("评价详情", res);
      var comment = res.data;
      comment.createTime = util.timeCalculation(new Date(comment.createTime));
      _this.setData({
        commentEntity: comment
      });
      //刷新上一页数据
      _this.refreshUpPageDate({
        type: 'commentCount',
        id: commentId,
        count: comment.commentCount
      });
    });
  },
  //预览图片
  previewImage(e) {
    var current = e.target.dataset.src;
    wx.previewImage({
      current: current, // 当前显示图片的http链接  
      urls: this.data.commentEntity.showImgs // 需要预览的图片http链接列表  
    })
  },
  //回复点赞
  replyThumbsUp(e) {
    console.log(e.currentTarget.dataset.index);
    let replyList = this.data.replyList;
    let item = replyList[e.currentTarget.dataset.index];
    item.thumbsUpCount = item.alreadyThumbs ? --item.thumbsUpCount : ++item.thumbsUpCount;
    item.alreadyThumbs = !item.alreadyThumbs;

    console.log(item)
    this.setData({
      replyList: replyList
    })
    Api.replyThumbsUp(item.id).then(res => {
      console.log(res);
    });
  },
  //打开评论输入框
  openComment() {
    this.setData({
      commentInputFlag: true
    });
  },
  //评论提交
  confirmCommentInput() {
    var _this = this;
    var value = this.data.commentEntity.value;
    console.log(value);
    if (!value) {
      wx.showToast({
        icon: 'none',
        title: '请输入具体内容',
      })
      return;
    }
    let requestData = {
      "isReplyUser": false,
      "topicId": this.data.commentEntity.id,
      "toUserId": "",
      "content": value
    };
    //提交评论
    Api.sbumitReply(requestData).then(res => {
      wx.showToast({
        title: '评论成功',
        icon: 'none',
      })
      let entity = 'commentEntity.value'
      _this.setData({
        replyPage: 1,
        replyList: [],
        replyTotalNum: [],
        replyTotalNum: 0,
        [entity]: '',
        commentInputFlag: false
      });
      //刷新数据
      _this.queryCommentDesc(this.data.commentEntity.id);
      _this.queryCommentReply(this.data.commentEntity.id);
    })
  },
  //评论框失去焦点事件
  hideCommentInput() {
    this.setData({
      commentInputFlag: false
    });
  },
  //回复框失去焦点事件
  hideReplyInput() {
    this.setData({
      replyFlag: false
    });
  },
  //评论主题点赞
  thumbComment() {
    var _this= this;
    var commentEntity = this.data.commentEntity;
    commentEntity.alreadyThumbs = !commentEntity.alreadyThumbs;
    this.setData({
      commentEntity: commentEntity
    });
    Api.commentThumbsUp(this.data.commentEntity.id).then(res => {
      console.log(res);
      _this.refreshUpPageDate({
        type:'thumb',
        id: commentEntity.id,
        thumb: commentEntity.alreadyThumbs
      });
    });
  },
  //显示回复输入框
  openReply(e) {
    console.log(e);
    var index = e.currentTarget.dataset.index;
    var item = this.data.replyList[index];
    //改变初始化属性
    this.setData({
      replyFlag: true,
      ["replyEntity.isReplyUser"]: true,
      ["replyEntity.topicId"]: this.data.commentEntity.id,
      ["replyEntity.toUserId"]: item.fromUserId,
      ["replyEntity.toUserName"]: item.fromUserName,
      ["replyEntity.content"]: ''
    });
  },
  //提交回复
  confirmReplyInput() {
    var _this = this;
    var content = _this.data.replyEntity.content;
    if (!content) {
      wx.showToast({
        icon: 'none',
        title: '请输入具体内容',
      })
      return;
    }
    //提交评论
    Api.sbumitReply(this.data.replyEntity).then(res => {
      wx.showToast({
        title: '评论成功',
        icon: 'none',
      })
      _this.setData({
        replyPage: 1,
        replyList:[],
        replyTotalNum: [],
        replyTotalNum: 0,
        replyFlag: false
      });
      //刷新数据
      _this.queryCommentDesc(this.data.commentEntity.id);
      _this.queryCommentReply(this.data.commentEntity.id);
    });
  },
  //实现页面双向绑定
  bindinput(e) {
    console.log(e);
    var dataKey = e.target.dataset.key;
    var value = e.detail.value;
    this.setData({
      [dataKey]: value
    });
    console.log(dataKey, value, this.data.commentEntity.value);
  },
  //显示底部加载动画
  showBottomLoding(){
    this.setData({
      ['loding.show']:true,
      ['loding.showText']: true,
      ['loding.text']: '加载中...',
    });
  },
  //隐藏底部加载动画
  hideBottomLoding() {
    this.setData({
      ['loding.show']: false,
      ['loding.showText']: false,
    });
  },
  //刷新上个页面数据
  refreshUpPageDate(data){
    var pages = getCurrentPages();//获取页面栈
    if (pages.length > 1) {
      //上一个页面实例对象
      var prePage = pages[pages.length - 2];
      console.log(prePage)
      //调用上一个页面的onShow方法
      prePage.refreshData(data)
    } 
  }
})